package com.izobnehc.rabbit.rabbitadmin.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.izobnehc.rabbit.rabbitframework.common.domain.Result;
import com.izobnehc.rabbit.rabbitgenerator.dto.GenerateParamDTO;
import com.izobnehc.rabbit.rabbitgenerator.mapper.CodeGenerateMapper;
import com.izobnehc.rabbit.rabbitgenerator.service.CodeGenerateService;
import com.izobnehc.rabbit.rabbitgenerator.vo.CodeGenerateVO;
import com.izobnehc.rabbit.rabbitgenerator.vo.TableVO;
import freemarker.template.TemplateException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 11272
 * @date 2023/10/16 18:30
 **/
@RestController
@RequestMapping("/codeGenerator")
@DS("slave_1")
public class CodeGeneratorController {

    private final CodeGenerateService codeGenerateService;

    private final CodeGenerateMapper codeGenerateMapper;

    public CodeGeneratorController(CodeGenerateService codeGenerateService, CodeGenerateMapper codeGenerateMapper) {
        this.codeGenerateService = codeGenerateService;
        this.codeGenerateMapper = codeGenerateMapper;
    }

    @PostMapping("/generate")
    public Result<CodeGenerateVO> connectionTest(@RequestBody GenerateParamDTO generateParamDTO) throws TemplateException, IOException {
        CodeGenerateVO codeGenerateVO = codeGenerateService.generate(generateParamDTO);
        return Result.success(codeGenerateVO);
    }

    @PostMapping("/tables")
    public Result<List<TableVO>> getTables() {
        List<String> tables = codeGenerateMapper.getTables();
        List<TableVO> tableVos = new ArrayList<>();
        tables.forEach(item -> tableVos.add(new TableVO(item)));
        return Result.success(tableVos);
    }
}
